﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Web.Common;
using Web.Filters;

namespace Web.Controllers.API
{
    [ApiAuthentication]
    public class BillController : ApiController
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        [AllowAnonymous, HttpPost]
        public IHttpActionResult Login([FromBody]Models.API.Param.LoginModel entity)
        {
            Result<Models.API.Result.LoginModel> result = new Result<Models.API.Result.LoginModel>() { Type = AjaxResultType.Error, Content = "", TotalRows = 0, Data = new Models.API.Result.LoginModel() };

            if (entity == null)
            {
                result.Content = "传输参数无效";
                return Json(result);
            }
            else if (String.IsNullOrWhiteSpace(entity.LoginName) || String.IsNullOrWhiteSpace(entity.LoginPass))
            {
                result.Content = "传输参数无效";
                return Json(result);
            }
            try
            {
                DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select Logins.ID,Users.Token,Logins.LoginPass,Logins.UserID,Users.Name UserName from Logins inner join Users on Logins.UserID=Users.ID where Logins.LoginType=1 and Logins.LoginName='" + entity.LoginName + "' and Logins.IsDeleted=0 and Users.IsDeleted=0").Tables[0];
                if (dt.Rows.Count != 1)
                {
                    result.Content = "登录名错误";
                }
                else if (dt.Rows[0]["LoginPass"].ToString() != entity.LoginPass)
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "密码错误";
                }
                else
                {
                    result.Data.LoginID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    result.Data.UserID = Convert.ToInt32(dt.Rows[0]["UserID"]);
                    result.Data.UserName = Convert.ToString(dt.Rows[0]["UserName"]);
                    result.Data.LoginName = entity.LoginName;
                    result.Data.Token = Convert.ToString(dt.Rows[0]["Token"]);
                    if (result.Data.Token == "" || FreeADO.DBUtility.DbHelperSQL.GetSingle("select OptionValue from Options where OptionKey='ApiLoginOnly'").ToString() == "1")
                    {
                        result.Data.Token = BaseWebApiExt.GetNewToken(result.Data.UserID);
                    }
                    result.Data.Token = BaseWebApiExt.ToCiphertextToken(result.Data.Token);
                    result.Content = "登录成功";
                    result.Type = AjaxResultType.Success;
                }
                return Json(result);

            }
            catch (Exception e)
            {
                result.Content = e.Message;
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult QueryIndexList([FromBody]Models.API.Param.IndexListModel entity)
        {
            try
            {
                SqlParameter[] parameters = {
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@BeginDate",SqlDbType.DateTime),
                                            new SqlParameter("@EndDate",SqlDbType.DateTime),
                                            new SqlParameter("@State",SqlDbType.Int),

                };
                parameters[0].Value = entity.BillNameID;
                parameters[1].Value = entity.PageIndex;
                parameters[2].Value = entity.PageSize;
                parameters[3].Value = entity.Search;
                parameters[4].Value = entity.BeginDate;
                parameters[5].Value = entity.EndDate;
                parameters[6].Value = entity.State;
                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBillIndexList", parameters, "aaa", 3000);

                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]), Data = ds.Tables[1] };
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult IndexEdit([FromBody]Models.API.Param.IndexEditModel entity)
        {
            try
            {
                Result<string> result = new Result<string>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = "" };
                switch (entity.BillNameID)
                {
                    case 1:
                        Models.MVC.Bill1IndexModel m1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill1IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill1.IndexEdit(m1);
                        break;
                    case 2:
                        Models.MVC.Bill2IndexModel m2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill2IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill2.IndexEdit(m2);
                        break;
                    case 3:
                        Models.MVC.Bill3IndexModel m3 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill3IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill3.IndexEdit(m3);
                        break;
                    case 4:
                        Models.MVC.Bill4IndexModel m4 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill4IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill4.IndexEdit(m4);
                        break;
                    case 5:
                        Models.MVC.Bill5IndexModel m5 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill5IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill5.IndexEdit(m5);
                        break;
                    case 6:
                        Models.MVC.Bill6IndexModel m6 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill6IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill6.IndexEdit(m6);
                        break;
                    case 7:
                        Models.MVC.Bill7IndexModel m7 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill7IndexModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill7.IndexEdit(m7);
                        break;
                    default:
                        break;
                }
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult IndexDel([FromBody]Models.API.Param.IndexEditModel entity)
        {
            Result<string> result = new Result<string>();
            try
            {
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE BillIndex" + entity.BillNameID.ToString() + " set IsDeleted=1 where ID in (" + entity.IDs + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";

                return Json(result);
            }
            catch (Exception e)
            {
                result = new Result<string>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = "" };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult ModifyIndexState([FromBody]Models.API.Param.IndexEditModel entity)
        {
            Result<decimal> result = new Result<decimal>() { Type = AjaxResultType.Success, Content = "成功", Data = 0, TotalRows = 0 };
            try
            {
                SqlParameter[] paras ={
                    new SqlParameter("@BillNameID",SqlDbType.Int),
                    new SqlParameter("@IndexID",SqlDbType.Int),
                    new SqlParameter("@Type",SqlDbType.Int),
                    new SqlParameter("@Str",SqlDbType.NVarChar,4000),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                                       };
                paras[0].Value = entity.BillNameID;
                paras[1].Value = entity.ID;
                paras[2].Value = entity.State;
                paras[3].Value = "";
                paras[3].Direction = ParameterDirection.InputOutput;
                paras[4].Direction = ParameterDirection.ReturnValue;

                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_ModifyIndexState", paras, "aaa", 3000);
                if (Convert.ToInt32(paras[4].Value) == 1)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = 0;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = paras[3].Value.ToString();
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult BillProceduresEdit([FromBody]Models.API.Param.BillProceduresModel entity)
        {
            Result<int> result = new Result<int>();
            try
            {
                if (entity.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillProcedures" + entity.BillNameID + " where BillIndexID=" + entity.IndexID + " and ShowIndex=" + entity.ShowIndex + " and IsDeleted=0")) > 0)
                    {
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql("UPDATE BillProcedures" + entity.BillNameID + " set ShowIndex=ShowIndex+1 where ShowIndex>=" + entity.ShowIndex + " and BillIndexID=" + entity.IndexID + " and IsDeleted=0");
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql("insert into BillProcedures" + entity.BillNameID + " (BillIndexID,ProcedureID,Remark,ShowIndex) values (" + entity.IndexID + "," + entity.ProcedureID + ",@Remark," + entity.ShowIndex + ")", new SqlParameter[1] { new SqlParameter("@Remark", entity.Remark) });
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillProcedures" + entity.BillNameID + " where BillIndexID=" + entity.IndexID + " and ShowIndex=" + entity.ShowIndex + " and IsDeleted=0 and ID<>" + entity.ID)) > 0)
                    {
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql("UPDATE BillProcedures" + entity.BillNameID + " set ShowIndex=ShowIndex+1 where ShowIndex>=" + entity.ShowIndex + " and BillIndexID=" + entity.IndexID + " and IsDeleted=0 and ID<>" + entity.ID);
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql("update BillProcedures" + entity.BillNameID + " set ProcedureID=" + entity.ProcedureID + ",Remark=@Remark,ShowIndex=" + entity.ShowIndex + @" where ID=" + entity.ID, new SqlParameter[1] { new SqlParameter("@Remark", entity.Remark) });
                }
                result.Type = AjaxResultType.Success;
                result.Content = "成功。";
                result.Data = 0;

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult ShowIndexEdit([FromBody]Models.API.Param.BillProceduresModel entity)
        {
            Result<int> result = new Result<int>();
            try
            {
                if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillProcedures"+entity.BillNameID+" where BillIndexID=" + entity.IndexID + " and ShowIndex=" + entity.ShowIndex + " and IsDeleted=0")) > 0)
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql("UPDATE BillProcedures" + entity.BillNameID + " set ShowIndex=ShowIndex+1 where ShowIndex>=" + entity.ShowIndex + " and BillIndexID=" + entity.IndexID + " and IsDeleted=0");
                }

                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"UPDATE BillProcedures" + entity.BillNameID + " set ShowIndex=" + entity.ShowIndex + " where ID=" + entity.ID);

                result.Type = AjaxResultType.Success;
                result.Content = "成功。";
                result.Data = 0;

            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult BillDetailsEdit([FromBody]Models.API.Param.DetailsEditModel entity)
        {
            try
            {
                Result<int> result = new Result<int>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = 0 };
                switch (entity.BillNameID)
                {
                    case 1:
                        Models.MVC.Bill1DetailsModel m1 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill1DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill1.BillDetailsEdit(m1);
                        break;
                    case 2:
                        Models.MVC.Bill1DetailsModel m2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill1DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill2.BillDetailsEdit(m2);
                        break;
                    case 3:
                        Models.MVC.Bill3DetailsModel m3 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill3DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill3.BillDetailsEdit(m3);
                        break;
                    case 4:
                        Models.MVC.Bill4DetailsModel m4 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill4DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill4.BillDetailsEdit(m4);
                        break;
                    case 5:
                        Models.MVC.Bill5DetailsModel m5 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill5DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill5.BillDetailsEdit(m5);
                        break;
                    case 6:
                        Models.MVC.Bill6DetailsModel m6 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill6DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill6.BillDetailsEdit(m6);
                        break;
                    case 7:
                        Models.MVC.Bill7DetailsModel m7 = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.MVC.Bill7DetailsModel>(Newtonsoft.Json.JsonConvert.SerializeObject(entity));
                        result = Web.Common.Bill7.BillDetailsEdit(m7);
                        break;
                    default:
                        break;
                }
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult QueryIndexModel([FromBody]Models.API.Param.IndexEditModel entity)
        {
            Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = null };
            DataTable dt = new DataTable();
            try
            {
                switch (entity.BillNameID)
                {
                    case 1:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex1.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex1 left join Goods on BillIndex1.GoodsID=Goods.ID left join Goods as SGoods on BillIndex1.SourceGoodsID=SGoods.ID
WHERE BillIndex1.ID=" + entity.ID).Tables[0];

                        break;
                    case 2:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex2.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex2 left join Goods on BillIndex2.GoodsID=Goods.ID left join Goods as SGoods on BillIndex2.SourceGoodsID=SGoods.ID
WHERE BillIndex2.ID=" + entity.ID).Tables[0];
                        break;
                    case 3:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex3.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex3 left join Goods on BillIndex3.GoodsID=Goods.ID left join Goods as SGoods on BillIndex3.SourceGoodsID=SGoods.ID
WHERE BillIndex3.ID=" + entity.ID).Tables[0];
                        break;
                    case 4:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex4.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex4 left join Goods on BillIndex4.GoodsID=Goods.ID left join Goods as SGoods on BillIndex4.SourceGoodsID=SGoods.ID
WHERE BillIndex4.ID=" + entity.ID).Tables[0];
                        break;
                    case 5:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex5.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex5 left join Goods on BillIndex5.GoodsID=Goods.ID left join Goods as SGoods on BillIndex5.SourceGoodsID=SGoods.ID
WHERE BillIndex5.ID=" + entity.ID).Tables[0];
                        break;
                    case 6:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex6.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex6 left join Goods on BillIndex6.GoodsID=Goods.ID left join Goods as SGoods on BillIndex6.SourceGoodsID=SGoods.ID
WHERE BillIndex6.ID=" + entity.ID).Tables[0];
                        break;
                    case 7:
                        dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select BillIndex7.*,isnull(Goods.Code,'') as GoodsCode,isnull(Goods.Name,'') as GoodsName,isnull(Goods.Model,'') as GoodsModel
				,isnull(SGoods.Code,'') as SourceGoodsCode,isnull(SGoods.Name,'') as SourceGoodsName,isnull(SGoods.Model,'') as SourceGoodsModel
FROM BillIndex7 left join Goods on BillIndex7.GoodsID=Goods.ID left join Goods as SGoods on BillIndex7.SourceGoodsID=SGoods.ID
WHERE BillIndex7.ID=" + entity.ID).Tables[0];
                        break;
                    default:
                        break;
                }
                result.Data = dt;
                return Json(result);
            }
            catch (Exception e)
            {
                 result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult QueryDetailsModel([FromBody]Models.API.Param.IndexEditModel entity)
        {
            Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = null };
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillDetails" + entity.BillNameID + " where ID=" + entity.ID).Tables[0];
            result.Data = dt;
            return Json(result);
        }


        [HttpPost]
        public IHttpActionResult QueryJiaoFuList([FromBody]Models.API.Param.BillProceduresModel entity)
        {
            try
            {
                DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select ID,CONVERT(nvarchar(50),CreateTime,120) as CreateTime,Qty 
from GoodsStockRunnings where BillNameID=" + entity.BillNameID + " and Type=1 and BillIndexID=" + entity.IndexID + @"
order by ID desc").Tables[0];

                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = dt.Rows.Count, Data = dt };
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult JiaoFuEdit([FromBody]Models.API.Param.JiaoFuEditModel entity)
        {
            Result<decimal> result = new Result<decimal>() { Type = AjaxResultType.Success, Content = "成功", Data = 0, TotalRows = 0 };
            try
            {
                SqlParameter[] paras ={
                    new SqlParameter("@ID",SqlDbType.Int),
                    new SqlParameter("@BillNameID",SqlDbType.Int),
                    new SqlParameter("@IndexID",SqlDbType.Int),
                    new SqlParameter("@Qty",SqlDbType.Int),
                    new SqlParameter("@Remark",SqlDbType.NVarChar,4000),
                    new SqlParameter("@Number",SqlDbType.NVarChar,4000),
                    new SqlParameter("@IsExec",SqlDbType.Int),
                    new SqlParameter("@Str",SqlDbType.NVarChar,4000),
                    new SqlParameter("@ReturnValue", SqlDbType.Int)
                                       };
                paras[0].Value = entity.ID;
                paras[1].Value = entity.BillNameID;
                paras[2].Value = entity.IndexID;
                paras[3].Value = entity.Qty;
                paras[4].Value = entity.Remark;
                paras[5].Value = entity.Number;
                paras[6].Value = entity.IsExec;
                paras[7].Value = "";
                paras[7].Direction = ParameterDirection.InputOutput;
                paras[8].Direction = ParameterDirection.ReturnValue;

                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_JiaoFuEdit", paras, "aaa", 3000);
                if (Convert.ToInt32(paras[8].Value) == 1)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = 0;
                }
                else if (Convert.ToInt32(paras[8].Value) == -1)
                {
                    result.Type = AjaxResultType.Warning;
                    result.Content = paras[7].Value.ToString();
                    result.Data = 0;
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = paras[7].Value.ToString();
                    result.Data = 0;
                }
            }
            catch (Exception e)
            {
                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult QueryStuffList([FromBody]Models.API.Param.StuffModel entity)
        {
            try
            {
                SqlParameter[] parameters = {
                                            new SqlParameter("@BillDetailsID",SqlDbType.Int),
            };
                parameters[0].Value = entity.BillDetailsID;
                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBill" + entity.BillNameID + "DetailsStuffList", parameters, "aaa", 3000);

                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]), Data = ds.Tables[1] };
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult QueryStaffModel([FromBody]Models.API.Param.IndexEditModel entity)
        {
            Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = null };
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query(@"
select a.Qty,a.GoodsID,b.TypeID,a.ID,"+entity.BillNameID+ @" as BillNameID,a.BillDetailsID
FROM BillDetailsStuff" + entity.BillNameID + @" as a inner join Goods b on a.GoodsID=b.ID
WHERE a.ID=" + entity.ID).Tables[0];
            result.Data = dt;
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult StaffEdit([FromBody]Models.API.Param.StuffModel entity)
        {
            Result<string> result = new Result<string>();
            try
            {
                if (entity.ID == 0)
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BillDetailsStuff" + entity.BillNameID + @"]
           ([BillDetailsID]
           ,[GoodsID]
           ,[Qty])
     VALUES
           (
'" + entity.BillDetailsID + @"',
'" + entity.GoodsID + @"',
'" + entity.Qty + @"'
)
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {

                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BillDetailsStuff" + entity.BillNameID + @"]
   SET [BillDetailsID] = '" + entity.BillDetailsID + @"'
      ,[GoodsID] = '" + entity.GoodsID + @"'
      ,[Qty] = '" + entity.Qty + @"'
 WHERE ID=" + entity.ID + @"
");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        public IHttpActionResult QueryDetailsList([FromBody]Models.API.Param.DetailsEditModel entity)
        {
            try
            {
                SqlParameter[] parameters = {
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@IndexID",SqlDbType.Int),

                };
                parameters[0].Value = entity.BillNameID;
                parameters[1].Value = entity.IndexID;
                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBillDetailsList", parameters, "aaa", 3000);

                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]), Data = ds.Tables[1] };
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }
        [HttpPost]
        public IHttpActionResult ProceduresDelAsync([FromBody]Models.API.Param.DetailsEditModel entity)
        {
            Result<string> result = new Result<string>();
            try
            {

                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete BillDetails"+entity.BillNameID+" where BillProcedureID in (" + entity.ID + @")");
                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete BillProcedures"+entity.BillNameID+" where ID in (" + entity.ID + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }

        [HttpPost]
        public IHttpActionResult DetailsDelAsync([FromBody]Models.API.Param.DetailsEditModel entity)
        {
            Result<string> result = new Result<string>();
            try
            {

                FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"Delete BillDetails" + entity.BillNameID + " where ID in (" + entity.ID + @")");
                result.Type = AjaxResultType.Success;
                result.Content = "成功";
                result.Data = "成功";
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult BillIndexSelectAsync([FromBody]Models.API.Param.IndexListModel entity)
        {
            try
            {

                SqlParameter[] parameters = {
                                            new SqlParameter("@BillNameID",SqlDbType.Int),
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,4000),
                                            new SqlParameter("@BeginDate",SqlDbType.DateTime),
                                            new SqlParameter("@EndDate",SqlDbType.DateTime),
                                            new SqlParameter("@State",SqlDbType.Int)};
                parameters[0].Value = entity.BillNameID;
                parameters[1].Value = entity.PageIndex;
                parameters[2].Value = entity.PageSize;
                parameters[3].Value = entity.Search;
                parameters[4].Value = "2000-01-01";
                parameters[5].Value = "2200-01-01";
                parameters[6].Value = 2;
                DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryBillIndexList", parameters, "aaa", 3000);
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = Convert.ToInt32(ds.Tables[0].Rows[0]["count"]), Data = ds.Tables[1] };
                return Json(result);
            }
            catch (Exception e)
            {
                Result<DataTable> result = new Result<DataTable>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = null };
                return Json(result);
            }
        }

    }
}
